//*****************************************************************************
//
// Copyright, (C) 2000-2005 Quantic Dream SA
//
// These coded instructions, statements and computer programs contain
// unpublished information, proprietary to Quantic Dream SA and are 
// protected by French and EEC copyright laws. They may not be 
// disclosed to third parties or copied or duplicated, in whole or in part, 
// without prior written consent of Quantic Dream SA
//
// Unpublished-rights reserved under the Copyright Laws of the EEC.
//
//*****************************************************************************
//
//	CLASS:	COLOR_PER_VERTEX_SHADER
//
//	JJA : 2012-07-23 - Created
//*****************************************************************************

#include "ColorPerVertexShader.h"
#include "Mesh/Mesh.h"
#include "Mesh/MeshInstance.h"
#include "Camera/Camera.h"

//=============================================================================
//	CODE STARTS HERE
//=============================================================================

//-----------------------------------------------------------------------------
//! @brief		COLOR_PER_VERTEX_SHADER constructor
//! @details		
//! @param		
//! @return		
//! @author		JJA : 2012-07-23
//-----------------------------------------------------------------------------
COLOR_PER_VERTEX_SHADER::COLOR_PER_VERTEX_SHADER()
:_uiModelViewProjectionMatrixUniformLocation( GL_INVALID_INDEX )
{
	
}

//-----------------------------------------------------------------------------
//! @brief		COLOR_PER_VERTEX_SHADER destructor
//! @details		
//! @param		
//! @return		
//! @author		JJA : 2012-07-23
//-----------------------------------------------------------------------------
COLOR_PER_VERTEX_SHADER::~COLOR_PER_VERTEX_SHADER()
{
	
}

//-----------------------------------------------------------------------------
//! @brief		Load
//! @details		
//! @param		
//! @return		
//! @author		JJA : 2012-07-25
//-----------------------------------------------------------------------------
void	COLOR_PER_VERTEX_SHADER::Load(const char *	pcVertexShaderPath,
									  const char *	pcFragmentShaderPath)
{
	SHADER::Load( pcVertexShaderPath, pcFragmentShaderPath );

	_uiModelViewProjectionMatrixUniformLocation = glGetUniformLocation( _uiShaderProgramId, "mModelViewProjectionMatrix" );
}

//-----------------------------------------------------------------------------
//! @brief		Begin
//! @details		
//! @param		
//! @return		
//! @author		JJA : 2012-07-26
//-----------------------------------------------------------------------------
void	COLOR_PER_VERTEX_SHADER::Begin() const
{
	glUseProgram( _uiShaderProgramId );

	glEnableVertexAttribArray( VERTEX_ATTRIB_POSITION );
	glEnableVertexAttribArray( VERTEX_ATTRIB_COLOR );
}

//-----------------------------------------------------------------------------
//! @brief		Apply
//! @details		
//! @param		
//! @return		
//! @author		JJA : 2012-07-26
//-----------------------------------------------------------------------------
void	COLOR_PER_VERTEX_SHADER::Apply(MESH_INSTANCE *	pMeshInstance, 
									   CAMERA *			pCamera) const
{
	const MATRIX4&	mModelMatrix			= pMeshInstance->GetWorldMatrix();
	const MATRIX4&	mViewMatrix				= pCamera->GetWorldMatrix();
	const MATRIX4&	mProjectionMatrix		= pCamera->GetProjectionMatrix();
	MATRIX4			mModelViewProjection	= mProjectionMatrix * mViewMatrix * mModelMatrix;

	glUniformMatrix4fv( _uiModelViewProjectionMatrixUniformLocation, 1, GL_FALSE, (GLfloat*)&mModelViewProjection );
}

//-----------------------------------------------------------------------------
//! @brief		End
//! @details		
//! @param		
//! @return		
//! @author		JJA : 2012-07-26
//-----------------------------------------------------------------------------
void	COLOR_PER_VERTEX_SHADER::End() const
{
	glDisableVertexAttribArray( VERTEX_ATTRIB_POSITION );
	glDisableVertexAttribArray( VERTEX_ATTRIB_COLOR );

	glUseProgram( 0 );
}

//=============================================================================
//	CODE ENDS HERE
//=============================================================================
